import {Injectable} from '@angular/core';
import {NzMessageService, NzNotificationService} from "ng-zorro-antd";
import {AjaxService} from "../../public/service/ajax.service";
import {SettingUrl} from "../../public/setting/setting_url";
import {HttpCodesEnum} from "../../public/setting/enums";

@Injectable({
  providedIn: 'root'
})
export class UserService {

  constructor(private notification: NzNotificationService,
              private ajaxService: AjaxService,
              private message: NzMessageService) {
  }

  /**
   * 查询User列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getUserList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.user.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 添加addLeader
   * @param params
   * @returns {Promise<T>}
   */
  addLeader(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.post({
        url: SettingUrl.URL.user.addLeader,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 登录用户账户
   * @param account
   * @returns {Promise<T>}
   */
  signInUser(account) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.login.signIn,
        data: {account:account},
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改User为领导人
   * @param code User编码
   * @param isLeader
   * @returns {Promise<T>}
   */
  updateToLeader(code, leader) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.user.updateToLeader,
        data: {
          code: code,
          leader: leader
        },
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改User密码
   * @returns {Promise<T>}
   */
  updateUserPwd(code, password) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.user.updatePwd,
        data: {
          code: code,
          password: password
        },
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 修改User
   * @returns {Promise<T>}
   */
  modify(code, state) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.put({
        url: SettingUrl.URL.user.modify,
        data: {
          code: code,
          state: state
        },
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            me.message.success(res.info);
            resolve(true);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询User详细信息
   * @param code User编码
   * @returns {Promise<T>}
   */
  loadUserByCode(code) {
    let me = this;
    return new Promise(function (resolve) {
      me.ajaxService.get({
        url: SettingUrl.URL.user.load + code,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * getAccount
   * @returns {any<T>}
   */
  getAccount(account) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.user.getAccount,
        data: {account: account},
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message);
        }
      });
    });
  }

  /**
   * 查询UserActive列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getUserActiveList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.userActive.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询UserBankCard列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getUserBankCardList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.userBankCard.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询UserGroup列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getUserGroupList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.userGroup.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询UserGroup详细信息
   * @param code UserGroup编码
   * @returns {Promise<T>}
   */
  loadUserGroupByCode(code) {
    let me = this;
    return new Promise(function (resolve) {
      me.ajaxService.get({
        url: SettingUrl.URL.userGroup.load + code,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询UserGroup详细信息
   * @param code UserGroup编码
   * @returns {Promise<T>}
   */
  loadByPreUserCode(preUserCode) {
    let me = this;
    return new Promise(function (resolve) {
      me.ajaxService.get({
        url: SettingUrl.URL.userGroup.loadByPreUserCode + preUserCode,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询OreRecord列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getOreRecordList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.oreRecord.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询UserJlb列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getUserJlbList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.userJlb.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询UserMiner列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getUserMinerList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.userMiner.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询UserMinerWorkRecord列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getUserMinerWorkRecordList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.userMinerWorkRecord.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询UserOre列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getUserOreList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.userOre.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询UserPurchaseMinerRecord列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getUserPurchaseMinerRecordList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.userPurchaseMinerRecord.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询UserSubAccount列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getUserSubAccountList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.userSubAccount.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询UserSubAccount详细信息
   * @param code UserSubAccount编码
   * @returns {Promise<T>}
   */
  loadUserSubAccountByCode(code) {
    let me = this;
    return new Promise(function (resolve) {
      me.ajaxService.get({
        url: SettingUrl.URL.userSubAccount.load + code,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          me.notification.error(`error`, res.message)
        }
      });
    })
  }

  /**
   * 查询UserDeposit列表
   * @param params {curPage:number,pageSize:number,name?:any)
   * @returns {any<T>}
   */
  getUserDepositList(params) {
    let me = this;
    return new Promise(function (resolve, reject) {
      me.ajaxService.get({
        url: SettingUrl.URL.userDeposit.list,
        data: params,
        success: (res) => {
          if (res.success && res.code === HttpCodesEnum.Success) {
            resolve(res.data);
          } else {
            reject(false);
            me.notification.error(`error`, res.info);
          }
        },
        error: (res) => {
          reject(false);
          me.notification.error(`error`, res.message)
        }
      });
    })
  }
}
